<?php

/**
 * This is the model class for table "{{offer}}".
 *
 * The followings are the available columns in table '{{offer}}':
 * @property integer $offerID
 * @property integer $vendorID
 * @property string $offer_detail
 * @property string $offer_value_percentage
 * @property string $offer_expiry_date
 * @property integer $offer_redeem_cityID
 * @property integer $offer_redeem_stateID
 * @property integer $offer_redeem_point
 * @property string $status
 * @property string $launch_date
 * @property string $offer_image_name
 * @property string $offer_image_thumb
 *
 * The followings are the available model relations:
 * @property Likes[] $likes
 * @property Vendor $vendor
 * @property City $offerRedeemCity
 * @property State $offerRedeemState
 * @property SeDiscountJob[] $seDiscountJobs
 */
class Offer extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Offer the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{offer}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('vendorID,featured, offer_type,offer_expiry_date,offer_value_percentage, offer_redeem_point, offer_title,launch_date,offer_redeem_cityID, offer_redeem_areaID, offer_redeem_stateID', 'required'),
			array('vendorID, offer_redeem_cityID, offer_redeem_stateID, offer_redeem_point,offer_value_percentage', 'numerical', 'integerOnly'=>true),
			array('offer_detail', 'length', 'max'=>500),
			array('offer_image_name, offer_image_thumb', 'length', 'max'=>45),
			array('offer_value_percentage','length','max'=>2),
			array('offer_title', 'length', 'max'=>100),
			array('status,offer_type', 'length', 'max'=>50),
			array('offer_expiry_date, launch_date', 'safe'),
			array('modified','default',
           	  'value'=>new CDbExpression('NOW()'),
              'setOnEmpty'=>false,'on'=>'update'),
        	array('created,modified','default',
              'value'=>new CDbExpression('NOW()'),
              'setOnEmpty'=>false,'on'=>'insert'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('offerID, vendorID, offer_type, offer_detail, offer_value_percentage, offer_expiry_date,  offer_redeem_cityID, offer_redeem_stateID, offer_redeem_point, status, launch_date, offer_image_name, offer_image_thumb', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'likes' => array(self::HAS_MANY, 'Likes', 'offerID'),
			'vendor' => array(self::BELONGS_TO, 'Vendor', 'vendorID'),
			'offerRedeemCity' => array(self::BELONGS_TO, 'City', 'offer_redeem_cityID'),
			'offerRedeemState' => array(self::BELONGS_TO, 'State', 'offer_redeem_stateID'),
			'seDiscountJobs' => array(self::HAS_MANY, 'SeDiscountJob', 'offerID'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'offerID' => 'Offer',
			'vendorID' => 'Vendor',
			'offer_title'=>'Offer Title',
			'offer_type' => 'Offer Type',
			'offer_detail' => 'Offer Detail',
			'offer_value_percentage' => 'Offer Value Percentage',
			'offer_expiry_date' => 'Offer Expiry Date',
			'offer_redeem_areaID' => 'Offer Redeem Area',
			'offer_redeem_cityID' => 'Offer Redeem City',
			'offer_redeem_stateID' => 'Offer Redeem State',
			'offer_redeem_point' => 'Offer Redeem Point',
			'status' => 'Status',
			'launch_date' => 'Launch Date',
			'offer_image_name' => 'Offer Image Name',
			'offer_image_thumb' => 'Offer Image Thumb',
			'featured'=>'Home Page Featured',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('offerID',$this->offerID);
		$criteria->compare('vendorID',$this->vendorID);
		$criteria->compare('offer_type',$this->offer_type,true);
		$criteria->compare('offer_detail',$this->offer_detail,true);
		$criteria->compare('offer_value_percentage',$this->offer_value_percentage,true);
		$criteria->compare('offer_expiry_date',$this->offer_expiry_date,true);
		$criteria->compare('offer_redeem_cityID',$this->offer_redeem_cityID);
		$criteria->compare('offer_redeem_stateID',$this->offer_redeem_stateID);
		$criteria->compare('offer_redeem_point',$this->offer_redeem_point);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('launch_date',$this->launch_date,true);
		$criteria->compare('offer_image_name',$this->offer_image_name,true);
		$criteria->compare('offer_image_thumb',$this->offer_image_thumb,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function getModel($type,$arrParams)
	{
		switch ($type)
		{
			case 'VENDOR-RAND-ID':
			return Offer::model()->find('vendorID=?',array($arrParams['vendorID']));
			break;
		}
	}
	
}